﻿namespace WMS6.Web.TransportManagement
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.TransportMgtServiceRef;
    using WMS6.Web.UserControls;

    public class DeliveryOrderList : BasePage
    {
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F8012;
        protected GridView gvDeliveryOrder;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2DeliveryOrderList(DeliveryOrderQueryEntity deliveryOrderQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(deliveryOrderQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = TransportMgtServiceClientProxy.GetInstance().GetDeliveryOrderByQueryEntity(deliveryOrderQuery);
            base.BindData2GridView(this.gvDeliveryOrder, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvDeliveryOrder, "DELIVERY_ORDER_ID");
                List<string> status = base.GetSelectedIDsInGridView(this.gvDeliveryOrder, "STATUS");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    if ((status != null) || (status.Count > 0))
                    {
                        for (int i = 0; i < status.Count; i++)
                        {
                            if (status[i].ToString().ToUpper() != "NEW")
                            {
                                this.lblMsg.Text = base.GetGlobalMsgResource("F8100_006");
                                return;
                            }
                        }
                    }
                    this.lblMsg.Text = TransportMgtServiceClientProxy.GetInstance().DeleteDeliveryOrder(selectedID) + base.GetGlobalMsgResource("C006");
                    DeliveryOrderQueryEntity deliveryOrderQuery = this.GetDeliveryOrderQueryEntity();
                    this.BindData2DeliveryOrderList(deliveryOrderQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvDeliveryOrder, "DELIVERY_ORDER_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("DeliveryOrder.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            DeliveryOrderQueryEntity deliveryOrderQuery = this.GetDeliveryOrderQueryEntity();
            deliveryOrderQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = deliveryOrderQuery.CurrentPage;
            deliveryOrderQuery.CurrentPage = "0";
            this.gvDeliveryOrder.Columns[1].Visible = false;
            this.gvDeliveryOrder.AllowSorting = false;
            this.BindData2DeliveryOrderList(deliveryOrderQuery);
            this.hdnActionID.Value = "";
            this.gvDeliveryOrder.ShowFooter = false;
            deliveryOrderQuery.IsGetAll = false;
            deliveryOrderQuery.CurrentPage = currentPageIndex;
            this.SetDeliveryOrderQueryEntity(deliveryOrderQuery);
            base.Export2Excel("DeliveryOrder.xls", this.gvDeliveryOrder);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            DeliveryOrderQueryEntity deliveryOrderQuery = this.GetDeliveryOrderQueryEntity();
            deliveryOrderQuery.CurrentPage = "0";
            this.BindData2DeliveryOrderList(deliveryOrderQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            DeliveryOrderQueryEntity deliveryOrderQuery = this.GetDeliveryOrderQueryEntity();
            deliveryOrderQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2DeliveryOrderList(deliveryOrderQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            DeliveryOrderQueryEntity deliveryOrderQuery = this.GetDeliveryOrderQueryEntity();
            deliveryOrderQuery.DeliveryOrderID = "";
            deliveryOrderQuery.ExternOrderID = "";
            deliveryOrderQuery.OwnerID = "";
            deliveryOrderQuery.DeliveryOrderType = "";
            deliveryOrderQuery.Priority = "";
            deliveryOrderQuery.Departure = "";
            deliveryOrderQuery.Destination = "";
            deliveryOrderQuery.TransferType = "";
            deliveryOrderQuery.PickupType = "";
            deliveryOrderQuery.PaymentType = "";
            deliveryOrderQuery.BillingType = "";
            deliveryOrderQuery.CreatedBy = "";
            this.BindData2DeliveryOrderList(deliveryOrderQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            DeliveryOrderQueryEntity deliveryOrderQuery = this.GetDeliveryOrderQueryEntity();
            if (int.Parse(deliveryOrderQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                deliveryOrderQuery.CurrentPage = (int.Parse(deliveryOrderQuery.CurrentPage) + 1).ToString();
                this.BindData2DeliveryOrderList(deliveryOrderQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            DeliveryOrderQueryEntity deliveryOrderQuery = this.GetDeliveryOrderQueryEntity();
            if (int.Parse(deliveryOrderQuery.CurrentPage) > 0)
            {
                deliveryOrderQuery.CurrentPage = (int.Parse(deliveryOrderQuery.CurrentPage) - 1).ToString();
                this.BindData2DeliveryOrderList(deliveryOrderQuery);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("DeliveryOrderQuery.aspx");
        }

        private DeliveryOrderQueryEntity GetDeliveryOrderQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as DeliveryOrderQueryEntity);
            }
            return (base.PageQueryEntitySession as DeliveryOrderQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                DeliveryOrderQueryEntity deliveryOrderQuery = this.GetDeliveryOrderQueryEntity();
                deliveryOrderQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2DeliveryOrderList(deliveryOrderQuery);
            }
        }

        protected void gvDeliveryOrder_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F8012", this.gvDeliveryOrder);
        }

        protected void gvDeliveryOrder_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvDeliveryOrder_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                DeliveryOrderQueryEntity deliveryOrderQuery = this.GetDeliveryOrderQueryEntity();
                deliveryOrderQuery.SortField = e.SortExpression;
                if (deliveryOrderQuery.SortDirection == "ASC")
                {
                    deliveryOrderQuery.SortDirection = "DESC";
                }
                else if (deliveryOrderQuery.SortDirection == "DESC")
                {
                    deliveryOrderQuery.SortDirection = "ASC";
                }
                this.BindData2DeliveryOrderList(deliveryOrderQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            DeliveryOrderQueryEntity deliveryOrderQuery = new DeliveryOrderQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                deliveryOrderQuery.DeliveryOrderID = this.PreviousPage.DeliveryOrderID;
                deliveryOrderQuery.ExternOrderID = this.PreviousPage.ExternOrderID;
                deliveryOrderQuery.OwnerID = this.PreviousPage.OwnerID;
                deliveryOrderQuery.DeliveryOrderType = this.PreviousPage.DeliveryOrderType;
                deliveryOrderQuery.Priority = this.PreviousPage.Priority;
                deliveryOrderQuery.Status = this.PreviousPage.Status;
                deliveryOrderQuery.Departure = this.PreviousPage.Departure;
                deliveryOrderQuery.Destination = this.PreviousPage.Destination;
                deliveryOrderQuery.TransferType = this.PreviousPage.TransferType;
                deliveryOrderQuery.PickupType = this.PreviousPage.PickupType;
                deliveryOrderQuery.PaymentType = this.PreviousPage.PaymentType;
                deliveryOrderQuery.BillingType = this.PreviousPage.BillingType;
                deliveryOrderQuery.CreatedBy = this.PreviousPage.CreateBy;
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            deliveryOrderQuery.IsGetAll = false;
            deliveryOrderQuery.CurrentPage = "0";
            deliveryOrderQuery.PageSize = this.gvDeliveryOrder.PageSize.ToString();
            deliveryOrderQuery.SortField = "DO.ROWID";
            deliveryOrderQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvDeliveryOrder.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvDeliveryOrder.Columns[1].Visible = false;
                this.gvDeliveryOrder.Columns[2].Visible = true;
                this.btnNew.Visible = false;
                this.btnEdit.Visible = false;
                this.btnDelete.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetDeliveryOrderQueryEntity(deliveryOrderQuery);
            this.BindData2DeliveryOrderList(deliveryOrderQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetDeliveryOrderQueryEntity(DeliveryOrderQueryEntity deliveryOrderQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = deliveryOrderQuery;
            }
            else
            {
                base.PageQueryEntitySession = deliveryOrderQuery;
            }
        }

        public DeliveryOrderQuery PreviousPage
        {
            get
            {
                return (DeliveryOrderQuery) base.PreviousPage;
            }
        }
    }
}

